package com.qin.controller;

import com.alibaba.fastjson.JSONObject;
import com.qin.entity.ExecuteBean;
import com.qin.entity.ExecuteLog;
import com.qin.entity.Function;
import com.qin.enums.RetCode;
import com.qin.service.ExecuteService;
import com.qin.thread.ExecuteFunctionThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller("executeController")
@RequestMapping("/execute")
@ResponseBody
public class ExecuteController {

    @Autowired
    private ExecuteService executeService;

    @RequestMapping(value="/executeFunction", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String executeFunction(@RequestBody String body){
//        System.out.println("web层：executeFunction");
        System.out.println("接口调用：/execute/executeFunction");

        //入参封装到实体类
        ExecuteBean executeBean = JSONObject.parseObject(body, ExecuteBean.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((executeBean.getVersion_date() == null) || ("".equals(executeBean.getVersion_date()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "version_date不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((executeBean.getFunction_id() == null) || ("".equals(executeBean.getFunction_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "function_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //新建ExecuteLog对象
        ExecuteLog executeLog = new ExecuteLog();
        executeLog.setFunction_id(executeBean.getFunction_id());
        //插入执行记录
        boolean isSuccess = executeService.insertExecuteRecord(executeLog);
        //获取execute_id，给到executeBean
        executeBean.setExecute_id(executeLog.getExecute_id());
        Map retMap = new HashMap();

        if(isSuccess == true){
            //启动线程执行函数
            ExecuteFunctionThread executeFunctionThread = new ExecuteFunctionThread(executeBean);
            executeFunctionThread.start();

            retMap.put("retCode", RetCode.SUCCESS.getCode());
            retMap.put("retMsg", RetCode.SUCCESS.getMsg());

//            try {
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        } else{
            retMap.put("retCode", RetCode.ERROR.getCode());
            retMap.put("retMsg", RetCode.ERROR.getMsg());
            retMap.put("errorMsg", "调用失败");
        }

        String retString = new JSONObject(retMap).toString();
        System.out.println("接口出参：" + retString);

        return retString;
    }

    @RequestMapping(value="/queryExecuteLog", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String queryExecuteLog(@RequestBody String body){
//        System.out.println("web层：queryExecuteLog");
        System.out.println("接口调用：/execute/queryExecuteLog");

        //入参封装到实体类
        ExecuteLog executeLog = JSONObject.parseObject(body, ExecuteLog.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((executeLog.getExecute_id() == null) || ("".equals(executeLog.getExecute_id()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "execute_id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = executeService.queryExecuteLog(executeLog).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }
}
